require('js/lib/require_patch').monkeypatch(this); // Needed for Android

var ui = require('js/ui/common/ui'),
	UtilityWindow = require('js/ui/UtilityWindow'),
	TextView = require('js/ui/TextView'),
	SettingsView = require('js/ui/SettingsView');

exports.MainView = function() {
	var instance = ui.view({
		backgroundColor:'#fff',
		backgroundImage: 'images/grain.png'
	});
	
	var AppointmentLabel = ui.label('', {top: 15, textAlign: 'center', color: '#fff'});		
	instance.add(AppointmentLabel);
	instance.updateAppointment = function(appointment) {
		AppointmentLabel.text = globals.util.getDateFromStr(appointment.dateTime).toDateString();
	}
	
	var btnView = ui.view({ top: 30, layout: 'vertcal' });
	instance.add(btnView);
	
	var categories = globals.db.listNoteCategories();
	var btnProps = {height: 35 * globals.deviceFactor, width: '60%', top: 10, backgroundImage: globals.util.urls()['bg_home']};
	var btnLoc = 10;
	
	// Create Note Window
	var params = { title:'', categoryId:-1, sequence:0};
	var view = new TextView(params);
	var win = new UtilityWindow(params.title, view, new SettingsView(), {top: 0, fullscreen: false});
	
	for (var i=0, l=categories.length; i<l; i++) {
		(function(){
			var params = { title:categories[i].name, categoryId:categories[i].id, sequence:categories[i].sequence};

			btnProps.top = btnLoc + (40 * globals.deviceFactor * i);

			if (i<l-1) 
				params.nextText = 'Next';
				
			btnView.add(ui.button(globals.util.objectMerge({title: params.title}, btnProps), 
				function(e){
					
					Ti.API.debug('Tab height is: ' +globals.tabs.height);
					win.updateTitle(params.title);
					var note = getNote(params.categoryId);
					if (note) {
						params = globals.util.objectMerge(params, note);
					}
					else {
						var PatientNote = require('js/model/PatientNote');
						params = globals.util.objectMerge(params, new PatientNote({id:'',text:'',note_type:params.categoryId}));
					}
					view.updateParams(params);
					
					view.addEventListener('app:dismiss.note', function(e){
						win.close();
					});
					view.addEventListener('app:next.note', function(e){
						var category = e.next;

						if (category) {
							var newParams = {categoryId: category.id, sequence: category.sequence, nextText: category.nextText, title: category.name };
							var note = getNote(category.id);
							if (note) {
								newParams = globals.util.objectMerge(newParams, note);
							}
							else {
								newParams.text = null, newParams.id = null;
							}
							view.updateParams(newParams);
							globals.util.debugDump('Moving to next note - params', newParams);
							win.updateTitle(newParams.title);
						}
					});
					
					Ti.API.debug('Active Tab is: '+globals.tabs.activeTab);
					globals.tabs.activeTab.add(win);
					win.open();
				}
			));
		})();
	}
	
		
	Ti.App.addEventListener('app.patient.change', function(e){
		win.updatePatient(globals.patient.fullName());
	});
	
	Ti.App.addEventListener('app.provider.change', function(e){
		win.updateProvider(globals.provider.firstName);
	});

		
	return instance;
					
};

function getNote(noteType) {
	var notes = globals.db.listPatientNotes(globals.patient.id, 'appointment_id = ? AND note_type = ?', [globals.appointment.id, noteType]);

	return notes[0]||null;
}

function getDateFromStr(dateStr) {
	Ti.API.debug('Converting ' + dateStr);

	var year = dateStr.substr(0,4), month = dateStr.substr(5,2), day = dateStr.substr(8,2), hour = dateStr.substr(11,2), minute = dateStr.substr(14,2), second = dateStr.substr(17,2);
	Ti.API.debug('Year: '+year+' Month: '+month+' Day: '+day+' Hour: '+hour+' Minute: '+minute+' Second: '+second);
	return new Date(year, month-1, day, hour, minute, second); // Month is relative to zero
}

